﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Modify;
using AssetsSys.Dto.Permission;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Auth;
using AssetsSys.WebApi.Models.Moidfy;
using AssetsSys.WebApi.Models.Permission;
using AssetsSys.WebApi.Models.Source;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 变更记录
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/modifylog")]
    public class ModifyController : ApiController
    {
        /// <summary>
        /// 获取变更记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public async Task<IHttpActionResult> GetModifyList([FromUri]ModifyQuertDto request)
        {
            var userId = ((UserIdentity)User.Identity).Id;
            var data = await ModifyManger.GetList(request, userId);
            return this.SendData(data);
        }

        /// <summary>
        /// 添加变更记录
        /// </summary>
        /// <param name="modifyid">记录Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("{modifyid}/infos")]
        public async Task<IHttpActionResult> CreateFixup(Guid modifyid,CreateModifylogViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            model.AssetsId = modifyid;
            var modifyId = ((UserIdentity) User.Identity).Id;
            return await ModifyManger.AddModify(model.AssetsId,model.NewStateId,model.ModifyDescribe,model.AcceptId,modifyId) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 完成变更
        /// </summary>
        /// <param name="modifyid">记录Id</param>
        /// <param name="modifyState">1:同意 2:拒绝</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{modifyid}/infos/{modifyState}")]
        public async Task<IHttpActionResult> UpdateModify(Guid modifyid,string modifyState)
        {
            return await ModifyManger.Update(modifyid, modifyState) ? this.SendData("变动成功") : this.ErrorData("变动失败"); ;
        }
    }
}
